﻿using Common.Contract;
using Common.Contract.CrossSelling;
using Common.Contract.CrossSelling.Record;
using Common.Contract.CrossSelling.Request;
using Common.Contract.CrossSelling.Response;
using Common.Contract.Customer;
using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request.ReduceFee;
using Common.Contract.Customer.Response.ReduceFee;
using Common.Service;
using SMECustomerWebsite.Core.Configuration;
using SMECustomerWebsite.Filters;
using SMECustomerWebsite.Models.ViewModels.CustomerDetail;
using SMECustomerWebsite.Models.ViewModels.ReduceRateFee;
using System.Collections.Generic;
using System.Web.Mvc;

namespace SMECustomerWebsite.Controllers
{
    public class CustomerDetailController : ControllerBase
    {
        [Authorize]
        [CustomActionFilter]
        public ActionResult Index(string cif)
        {
            CustomerReduceFeeInfoRecord resultCustomer = new CustomerReduceFeeInfoRecord();
            CustomerInfoViewModel vm = new CustomerInfoViewModel();
            vm.CurrentUser = CurrentUserRecord;
            if (!string.IsNullOrEmpty(cif))
            {
                resultCustomer = GetCustInfoByCif(cif);
            }

            vm.Customer = resultCustomer;
            return View(vm);
        }

        [Authorize]
        [CustomActionFilter]
        public ActionResult ProductHolding()
        {
            return View();
        }

        [ChildActionOnly]
        [Authorize]
        public ActionResult CustomerInfo(string cif)
        {
            CustomerReduceFeeInfoRecord resultCustomer = new CustomerReduceFeeInfoRecord();
            CustomerInfoViewModel vm = new CustomerInfoViewModel();
            vm.CurrentUser = CurrentUserRecord;
            if (!string.IsNullOrEmpty(cif))
            {
                resultCustomer = GetCustInfoByCif(cif);
            }
            vm.Customer = resultCustomer;
            return PartialView(vm.Customer.custInfo);
        }

        [ChildActionOnly]
        [Authorize]
        public ActionResult CustomerAlert(string cif)
        {
            return PartialView();
        }

        [ChildActionOnly]
        [Authorize]
        public ActionResult CustomerXSelling(string cif)
        {
            CustomerCrossellingProductDetailRecord resultCustomer = new CustomerCrossellingProductDetailRecord();
            using (ServiceClient<ICrossSelling> serviceClient = new ServiceClient<ICrossSelling>())
            {
                RetrieveProductXSellingByCifRequest request = new RetrieveProductXSellingByCifRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ReduceRateFee.GetCustInfoByCif"
                    },
                    CurrentUser = CurrentUserRecord,
                    Lang = CurrentLang,
                    Cif = cif
                };
                var customerRecords = new RetrieveProductXSellingByCifResponse();
                customerRecords = serviceClient.Proxy.RetrieveProductXSellingByCif(request);
                resultCustomer = (customerRecords == null || customerRecords.Record == null) ? new CustomerCrossellingProductDetailRecord() : customerRecords.Record;
            }
            CustomerDetailXsellingViewModel vm = new CustomerDetailXsellingViewModel
            {
                listProduct = resultCustomer
            };
            return PartialView(vm);
        }

        [Authorize]
        private CustomerReduceFeeInfoRecord GetCustInfoByCif(string cif)
        {
            CustomerReduceFeeInfoRecord resultCustomer = new CustomerReduceFeeInfoRecord();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerReduceInfoRequest request = new RetrieveCustomerReduceInfoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ReduceRateFee.GetCustInfoByCif"
                    },
                    CurrentUser = CurrentUserRecord,
                    Lang = CurrentLang,
                    Cif = cif
                };
                var customerRecords = new RetrieveCustomerReduceInfoResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerReduceInfoByCif(request);
                resultCustomer = (customerRecords == null || customerRecords.Record == null) ? new CustomerReduceFeeInfoRecord() : customerRecords.Record;
            }
            return resultCustomer;
        }
    }
}
